﻿/**
*	@name							Tabify
*	@descripton						Tabbed content with ease
*	@version						1.4
*	@requires						Jquery 1.3.2
*
*	@author							Jan Jarfalk
*	@author-email					jan.jarfalk@unwrongest.com
*	@author-twitter					janjarfalk
*	@author-website					http://www.unwrongest.com
*
*	@licens							MIT License - http://www.opensource.org/licenses/mit-license.php
*/

var itemUI = "<div class=\"itemUI\"><div style=\"height: 0; display: {4}\"><img alt=\"{1}\" src=\"Images/GoodStatus.png\" style=\"z-index: 1; top: 125px; position: relative;\" /><br/><span style=\"z-index: 2; position: relative; color: #a900e0; top: 100px\">Second hand {5}%</span></div><div class=\"image\"><img alt=\"title\" src=\"uploads/{0}\" /></div><div class=\"name\">{1}</div><img alt=\"{1}\" src=\"Images/new.gif\" style=\"display: {3}\" /><div class=\"price\">{2}</div></div>";

var rectItemUI = "<div class=\"rectItemUI\"><div class=\"image\"><img alt=\"{1}\" src=\"uploads/{0}\" /></div><div class=\"description\"><div style=\"text-align: justify; display: {4}\"><span style=\"color: #a900e0;\">** Tình trạng {5}%</span></div><div class=\"name\">{1}</div><img alt=\"{1}\" src=\"Images/new.gif\" style=\"display: {3}\" /><div class=\"price\">{2}</div></div></div>";

String.format = function(text) {

    //check if there are two arguments in the arguments list

    if (arguments.length <= 1) {

        //if there are not 2 or more arguments there's nothing to replace

        //just return the original text

        return text;

    }

    //decrement to move to the second argument in the array

    var tokenCount = arguments.length - 2;

    for (var token = 0; token <= tokenCount; token++) {

        //iterate through the tokens and replace their placeholders from the original text in order

        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"),

                                                arguments[token + 1]);

    }

    return text;

};

(function($) {
    $.fn.extend({
        tabify: function(callback) {

            function getHref(el) {
                hash = $(el).find('a').attr('href');
                hash = hash.substring(0, hash.length - 4);
                return hash;
            }

            function setActive(el) {

                $(el).addClass('active');
                $(getHref(el)).show();
                $(el).siblings('li').each(function() {
                    $(this).removeClass('active');
                    $(getHref(this)).hide();
                });
            }

            return this.each(function() {

                var self = this;
                var callbackArguments = { 'ul': $(self) };


                $(this).find('li a').each(function() {
                    $(this).attr('href', $(this).attr('href') + '-tab');
                });

                function handleHash() {

                    if (location.hash && $(self).find('a[href=' + location.hash + ']').length > 0) {
                        setActive($(self).find('a[href=' + location.hash + ']').parent());
                    }
                }

                if (location.hash) {
                    handleHash();
                }

                //                setInterval(handleHash, 100);

                $(this).find('li').each(function() {

                    var self = this;

                    function loadProducts(id, urlName) {
                        $.ajax({
                            type: "POST",
                            url: "WebService.asmx/LoadProducts",
                            data: "{ categoryID: '" + id + "' }",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function(response) {
                                var products = response.d;
                                if (products != null && products.length > 0) {
                                    var html = "";
                                    for (k = 0; k < products.length; k++) {
                                        if (k == 0)
                                            html += String.format(itemUI, products[k].ProductImage, products[k].AbbrDescription, products[k].Price, (products[k].NewStyle == "block" ? "inline" : "none"), products[k].SecondHandStyle, products[k].OldPercent);
                                        else
                                            html += String.format(rectItemUI, products[k].ProductImage, products[k].AbbrDescription, products[k].Price, (products[k].NewStyle == "block" ? "inline" : "none"), products[k].SecondHandStyle, products[k].OldPercent);
                                    }
                                }
                                $(urlName).empty();
                                $(urlName).append(html);
                            },
                            failure: function(response) {
                                alert(response.d);
                            }
                        });
                    }

                    function initload() {
                        var url = $(self).find('a').attr('href');
                        var urlName = "div" + url.replace("-tab", "");
                        window.location = url;
                        if ($(urlName).html() == "")
                            loadProducts(self.id, urlName);
                    }

                    $(this).mouseover(function() {
                        initload();
                        handleHash();
                    });

                    if ($(this).hasClass('active')) {
                        $(getHref(this)).show();
                    } else {
                        $(getHref(this)).hide();
                    }

                    initload();
                });

                if (callback) {
                    callback(callbackArguments);
                }

            });
        },
        tabify_new: function(callback) {

            function getHref(el) {
                hash = $(el).find('a').attr('href');
                hash = hash.substring(0, hash.length - 4);
                return hash;
            }

            function setActive(el) {

                $(el).addClass('active');
                $(getHref(el)).show();
                $(el).siblings('li').each(function() {
                    $(this).removeClass('active');
                    $(getHref(this)).hide();
                });
            }

            return this.each(function() {

                var self = this;
                var callbackArguments = { 'ul': $(self) };


                $(this).find('li a').each(function() {
                    $(this).attr('href', $(this).attr('href') + '-tab');
                });

                function handleHash() {

                    if (location.hash && $(self).find('a[href=' + location.hash + ']').length > 0) {
                        setActive($(self).find('a[href=' + location.hash + ']').parent());
                    }
                }

                if (location.hash) {
                    handleHash();
                }

                //                setInterval(handleHash, 100);

                $(this).find('li').each(function() {

                    var self = this;

                    function loadProducts(id, urlName) {
                        $.ajax({
                            type: "POST",
                            url: "WebService.asmx/LoadProducts",
                            data: "{ categoryID: '" + id + "' }",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function(response) {
                                var products = response.d;
                                if (products != null && products.length > 0) {
                                    var html = "";
                                    for (k = 0; k < products.length; k++) {
//                                        if (k == 0)
                                            html += String.format(itemUI, products[k].ProductImage, products[k].AbbrDescription, products[k].Price, (products[k].NewStyle == "block" ? "inline" : "none"), products[k].SecondHandStyle, products[k].OldPercent);
//                                        else
                                            //                                            html += String.format(rectItemUI, products[k].ProductImage, products[k].AbbrDescription, products[k].Price, (products[k].NewStyle == "block" ? "inline" : "none"), products[k].SecondHandStyle, products[k].OldPercent);
                                    }
                                }
                                $(urlName).empty();
                                $(urlName).append(html);
                            },
                            failure: function(response) {
                                alert(response.d);
                            }
                        });
                    }

                    function initload() {
                        var url = $(self).find('a').attr('href');
                        var urlName = "div" + url.replace("-tab", "");
                        window.location = url;
                        if ($(urlName).html() == "")
                            loadProducts(self.id, urlName);
                    }

                    $(this).mouseover(function() {
                        initload();
                        handleHash();
                    });

                    if ($(this).hasClass('active')) {
                        $(getHref(this)).show();
                    } else {
                        $(getHref(this)).hide();
                    }

                    initload();
                });

                if (callback) {
                    callback(callbackArguments);
                }

            });
        },
        tabify_sh: function(callback) {

            function getHref(el) {
                hash = $(el).find('a').attr('href');
                hash = hash.substring(0, hash.length - 4);
                return hash;
            }

            function setActive(el) {

                $(el).addClass('active');
                $(getHref(el)).show();
                $(el).siblings('li').each(function() {
                    $(this).removeClass('active');
                    $(getHref(this)).hide();
                });
            }

            return this.each(function() {

                var self = this;
                var callbackArguments = { 'ul': $(self) };


                $(this).find('li a').each(function() {
                    $(this).attr('href', $(this).attr('href') + '-tab');
                });

                function handleHash() {

                    if (location.hash && $(self).find('a[href=' + location.hash + ']').length > 0) {
                        setActive($(self).find('a[href=' + location.hash + ']').parent());
                    }
                }

                if (location.hash) {
                    handleHash();
                }

                //                setInterval(handleHash, 100);

                $(this).find('li').each(function() {

                    var self = this;

                    function loadProducts(id, urlName) {
                        $.ajax({
                            type: "POST",
                            url: "WebService.asmx/LoadProducts",
                            data: "{ categoryID: '" + id + "' }",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function(response) {
                                var products = response.d;
                                if (products != null && products.length > 0) {
                                    var html = "";
                                    for (k = 0; k < products.length; k++) {
//                                        if (k == 0)
//                                            html += String.format(itemUI, products[k].ProductImage, products[k].AbbrDescription, products[k].Price, (products[k].NewStyle == "block" ? "inline" : "none"), products[k].SecondHandStyle, products[k].OldPercent);
//                                        else
                                        html += String.format(rectItemUI, products[k].ProductImage, products[k].AbbrDescription, products[k].Price, (products[k].NewStyle == "block" ? "inline" : "none"), products[k].SecondHandStyle, products[k].OldPercent);
                                    }
                                }
                                $(urlName).empty();
                                $(urlName).append(html);
                            },
                            failure: function(response) {
                                alert(response.d);
                            }
                        });
                    }

                    function initload() {
                        var url = $(self).find('a').attr('href');
                        var urlName = "div" + url.replace("-tab", "");
                        window.location = url;
                        if ($(urlName).html() == "")
                            loadProducts(self.id, urlName);
                    }

                    $(this).mouseover(function() {
                        initload();
                        handleHash();
                    });

                    if ($(this).hasClass('active')) {
                        $(getHref(this)).show();
                    } else {
                        $(getHref(this)).hide();
                    }

                    initload();
                });

                if (callback) {
                    callback(callbackArguments);
                }

            });
        }
    });
})(jQuery);